package ml.tewas.commondata;

import java.awt.image.BufferedImage;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import ml.tewas.gui.DrawingCanvas;
import ml.tewas.util.Utilities;

public class ThreatStruct implements IThreat {

	// Cells passed in each move
	private int speed;

	private BoardCell position;

	private ThreatOrientationEnum orientation;

	private Map<Integer, ThreatClassificationEnum> classifications;

	private BufferedImage image;
	
	private boolean destroyed = false;
	
	private boolean exploded = false;
	
	private List<BoardCell> agentsPosiList = null;
	
	private int threatID;
	

	public ThreatStruct(int threatID,int speed, BoardCell position,
			ThreatOrientationEnum orientation, BufferedImage image) {
		super();
		this.threatID = threatID;
		this.speed = speed;
		this.position = position;
		this.orientation = orientation;
		this.classifications = new HashMap<Integer, ThreatClassificationEnum>();
		this.image = image;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see threatevaluation.IThreat#getClassification()
	 */
	public ThreatClassificationEnum getClassification(int agentID) {
		return classifications.get(agentID);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see threatevaluation.IThreat#setClassification(threatevaluation.ThreatClassificationEnum)
	 */
	public void setClassification(int agentID,
			ThreatClassificationEnum classification) {
		this.classifications.put(agentID, classification);
	}
	
	public void setClassifications(Map<Integer, ThreatClassificationEnum> classifications){
		this.classifications = classifications;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see threatevaluation.IThreat#getOrientation()
	 */
	public ThreatOrientationEnum getOrientation() {
		return orientation;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see threatevaluation.IThreat#getSpeed()
	 */
	public int getSpeed() {
		return speed;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see threatevaluation.IThreat#getPosition()
	 */
	public BoardCell getPosition() {
		return position;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see threatevaluation.IThreat#setPosition(threatevaluation.BoardCell)
	 */
	public void setPosition(BoardCell position) {
		this.position = position;
	}

	public BufferedImage getImage() {
		return image;
	}

	public void setImage(BufferedImage image) {
		this.image = image;
	}

	public void tick() {
		if(destroyed)
			return;
		if(!Utilities.checkPosition(position)){
			destroyed=true;
			return;
		}
		Utilities.updatePosition(position,speed,orientation);
		checkGameEnd();
	}
	private void checkGameEnd() {

		for(BoardCell agentPos: agentsPosiList){
			if(agentPos.getCellXPosition() == position.getCellXPosition() && agentPos.getCellYPosition() == position.getCellYPosition()){
				DrawingCanvas.getInstance().setState(DrawingCanvas.END_LOST);
				exploded = true;
			}
		}
		
	}

	public boolean isDestroyed() {
		return destroyed;
	}

	public void setDestroyed(boolean isDestroyed) {
		
		destroyed = isDestroyed;
	}

	public void setAgentsPositions(List<BoardCell> list) {
		
		agentsPosiList = list;
		
	}

	public boolean isExploded() {
		return exploded;
	}

	public int getThreatID() {
		return threatID;
	}
	
	
}